#include "importdefsdialog.h"

ImportDefsDialog::ImportDefsDialog()
{
setupUi(this);

QPushButton * okButton = qobject_cast<QPushButton *>( buttonBox->button( QDialogButtonBox::Ok ) );
    if (okButton != NULL)
    {
    okButton->setDisabled(true);
    }

QObject::connect( openToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( file_name() ) );
QObject::connect( emptyToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( save_empty() ) );
}

ImportDefsDialog::~ImportDefsDialog()
{
}

void ImportDefsDialog::file_name()
{
QString path = QFileDialog::getOpenFileName( this,
                                             tr("Select XML file"),
                                             QDir::homePath(),
                                             tr("MS XML Spreadsheet Files (*.xml);;All Files (*)"));

    if ( true == path.isEmpty() )
    {
    fileLineEdit->clear();
    file_path = QLatin1String("");
    return;
    }
    else
    {
    fileLineEdit->setText( QDir::toNativeSeparators(path) );
    }

file_path = QDir::toNativeSeparators(path);
QPushButton * okButton = qobject_cast<QPushButton *>( buttonBox->button( QDialogButtonBox::Ok ) );
QFile file(file_path);

    if ( true == file.open( QFile::ReadOnly | QFile::Text ) )
    {
        if (okButton != NULL)
        {
        okButton->setEnabled(true);
        }    
    }
    else
    {
    file_path = QLatin1String("");
    fileLineEdit->clear();
    QMessageBox::critical( this,
                           tr("Error"),
                           tr("Could not open file.") );
        if (okButton != NULL)
        {
        okButton->setDisabled(true);
        } 
    }

file.close();
}

QString ImportDefsDialog::return_selection()
{
return file_path;
}

bool ImportDefsDialog::write_byte_array( const QString & file_name,
                                 const QByteArray & ba )
{
QFile file_(file_name);
    if ( file_.exists() )
    {
    QMessageBox mBox;
    mBox.setWindowTitle( tr("File exists") );
    mBox.setIcon( QMessageBox::Question );
    mBox.setText( tr("Overwrite?") );
    mBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No );
        switch ( mBox.exec() )
        {
            case QMessageBox::Yes:
            break;

            default:
            return false;
            break;
        }
    file_.open(QIODevice::WriteOnly);
    file_.remove();
    }
QFile file(file_name);
bool opened = file.open(QIODevice::WriteOnly);
    if (true != opened)
    {
    return false;
    }
qint64 bytes = Q_INT64_C(-1);
bytes = file.write(ba);
QString message = tr("Wrote");
message.append( QLatin1String(" ") );
message.append( QString::number(bytes) );
message.append( QLatin1String(" ") );
message.append( tr("bytes") );
bool success = file.flush();
file.close();
return success;
}

void ImportDefsDialog::save_empty()
{
QFile buildin_file(":/binary/definitions.xml");
QByteArray buildin_file_ba;

bool ok0 = false;
ok0 = buildin_file.open( QIODevice::ReadOnly );
    if ( true == ok0 )
    {
    buildin_file_ba = buildin_file.readAll();
    }
    else
    {
    QMessageBox::critical( this,
                          tr("Error"),
                          tr("Internal error. File is not available.") );
    return;
    }

QByteArray & buildin_file_ba_ref = buildin_file_ba;
buildin_file.close();

QString suggested_path;
QString home_path = QDir::homePath();
QString desktop_path = home_path;
desktop_path.append( QDir::separator() );
desktop_path.append( QLatin1String("Desktop") );
QDir desktop_dir(desktop_path);
    if ( true == desktop_dir.exists() )
    {
    suggested_path = desktop_path;
    }
    else
    {
    suggested_path = home_path;
    }
suggested_path.append( QDir::separator() );
suggested_path.append( QLatin1String("definitions.xml") );

QString path = QFileDialog::getSaveFileName( this,
                                             tr("Select XML file"),
                                             QDir::toNativeSeparators(suggested_path),
                                             tr("MS XML Spreadsheet Files (*.xml);;All Files (*)"));

    if ( true == path.isEmpty() )
    {
    QMessageBox::critical( this,
                          tr("Error"),
                          tr("Empty file name.") );
    return;
    }
    else
    {
    bool ok1 = false;
    ok1 = write_byte_array(path, buildin_file_ba_ref);
        if ( true == ok1 )
        {
        QMessageBox::information(this,
                                 tr("Information"),
                                 tr("Open the file with Excel or OpenOffice. Add definitions into appropriate worksheets. Don\'t change the names of the the worksheets.") );
        }
        else
        {
        QMessageBox::critical(this,
                              tr("Error"),
                              tr("Could not save file.") );
        }
    }

}
